import { reqLogin, reqUserInfo } from '@/api/login.js';
import { UserContext } from "@/utils/token.js";
import { defineStore } from 'pinia';
// 引入路由
import { anyRoute, asyncRoute, constantRoute } from '@/router/routes';

import router from '@/router';
import _ from 'lodash';
const cloneDeep = _.cloneDeep;

//用于过滤当前用户需要展示的异步路由
function filterAsyncRoute(asyncRoute, routes) {
    return asyncRoute.filter((item) => {
        if (routes.includes(item.name)) {
            if (item.children && item.children.length > 0) {
                // 硅谷333账号: product\trademark\attr\sku
                item.children = filterAsyncRoute(item.children, routes);
            }
            return true;
        }
    })
}

// 创建用户小仓库
let useUserStore = defineStore('User', {
    // 小仓库存储数据地方
    state: () => {
        return {
            token: UserContext.getToken(),
            storeId: null,
            username: null,
            permission: null,
            menuRoutes: constantRoute,
            avatar: '',
            disabledButtons:false
        }
    },
    //异步|逻辑的地方
    actions: {
        // 用户登录的方法
        async userLogin(data) {
            //登录请求
            let result = await reqLogin(data);
            if (result === null) {
                return false
            }

            // pinia仓库存储一下token
            this.token = result.data.token
            this.permission = result.data.permission
            // 本地存储持久化存储一份
            UserContext.setToken(result.data.token);

            return true
        },
        // 获取用户信息方法
        async userInfo() {
            let result = await reqUserInfo();
            if (result !== null) {
                // 获取用户信息成功
                this.storeId = result.data.storeId
                this.username = result.data.username
                this.permission = result.data.permission

                let routes = []
                switch (this.permission) {
                    case 0:
                        routes = [
                            "menu-store", "store", "product", "log-product-number",
                            "menu-personnel", "clerk", "customer",
                            "menu-voucher", "voucher",
                            "menu-deal", "deal-product", "log-deal-product-number", "log-deal-product-payment", "deal-voucher",
                        ]
                        break;
                    case 1:
                        routes = [
                            "menu-store", "product", "log-product-number",
                            "menu-personnel", "customer",
                            "menu-voucher", "voucher",
                            "menu-deal", "deal-product", "log-deal-product-number", "log-deal-product-payment", "deal-voucher",
                            "menu-settings", "settings",
                        ]
                        break;
                    case 2:
                        routes = [
                            "menu-personnel", "customer",
                            "menu-voucher",
                            "menu-deal", "deal-product", "log-deal-product-number", "log-deal-product-payment", "deal-voucher",
                            "menu-settings", "settings",
                        ]
                        this.disabledButtons = true
                        break
                    default:
                        return false
                }

                // 计算当前用户的异步路由
                // let userAsyncRoute = filterAsyncRoute(cloneDeep(asyncRoute), result.data.routes);
                let userAsyncRoute = filterAsyncRoute(cloneDeep(asyncRoute), routes);
                this.menuRoutes = [...constantRoute, ...userAsyncRoute, anyRoute];
                [...userAsyncRoute, anyRoute].forEach((route) => {
                    router.addRoute(route);
                });

                return true
            } else {
                // 获取用户信息失败
                // return Promise.reject(new Error("获取用户信息失败"));
                return false
            }
        },
        //退出登录
        async userLogout() {
            this.token = null
            this.storeId = null
            this.username = null
            this.permission = null
            this.disabledButtons = false
            UserContext.removeToken()

            return true
        },
    },
    getters: {
    }
})

//对外暴露获取小仓库方法
export default useUserStore;
